package com.hc.server.actions;

import java.text.ParseException;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.json.JSONException;

import com.hc.server.Config;
import com.hc.server.Error;
import com.hc.server.IAction;
import com.hc.server.Request;
import com.hc.server.Response;
import com.hc.server.conn.mall.MallResponse;
import com.hc.server.conn.mall.MallServerHelper;
import com.hc.server.db.DBFactory;
import com.hc.server.db.Mporder;
import com.hc.server.db.Ordermap;
import com.hc.server.util.Util;

/**
 * 支付通知接口
 * @author xujh
 *
 */
public class BILL_RESULT implements IAction {

//	private static final String TRANS_ID = "TRANS_ID";
	private static final String THIRD_ID = "THIRD_ID";
//	private static final String BILL_ID = "BILL_ID";
	private static final String SETTLE_TIME = "SETTLE_TIME";//结算时间
	private static final String AMOUNT = "AMOUNT";
//	private static final String PHONE = "PHONE";
//	private static final String PAY_TIME = "PAY_TIME";//支付时间
//	private static final String ADDITION = "ADDITION";

	@Override
	public void execute(Request req, Response resp) {
		//取参数
		int orderId;
		//int transId;
		Date settleTime;
		int amount;
		try {
			orderId = req.getIntArg(THIRD_ID);
			//transId = req.getIntArg(TRANS_ID);
			settleTime = Util.Date2SQLDate(req.getStrArg(SETTLE_TIME));
			amount = req.getIntArg(AMOUNT);
		} catch (ParseException e) {
			//FIXME:独立的错误码
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		} catch (JSONException e) {
			resp.setErrResp(Error.INVALID_ARGS_ERR_CD, Error.INVALID_ARGS_ERR_MSG);
			return;
		}
		
		Session session = DBFactory.openSession();
		Transaction t = session.beginTransaction();
		try{
			t.begin();
			Mporder order = (Mporder) session.load(Mporder.class, new Integer(orderId));
			if(null == order){
				resp.setErrResp(Error.ORDER_NOFOUND_ERR_CD, Error.ORDER_NOFOUND_ERR_MSG);
				return;
			}
			//只有在待支付状态的订单才能修改
			int status = order.getStatus();
			if(Config.ORDER_PAYING == status){
				if(amount != order.getAmount()){
					//TODO 支付金额和应收金额不匹配，增加报警机制
					resp.setErrResp(Error.ORDER_AMOUNT_ERR_CD, Error.ORDER_AMOUNT_ERR_MSG);
					return;
				}
				Set<Ordermap> maps = order.getOrdermaps();
				Iterator<Ordermap> mapIter = maps.iterator();
				while(mapIter.hasNext()){
					Ordermap map = mapIter.next();
					//修改商城订单状态
					MallResponse mallResp = MallServerHelper.payOrder(map.getMallorderId());
					if(0 != mallResp.getRetCode()){
						//TODO，如果修改商城订单状态异常，增加报警机制
					}
				}
				//修改订单状态
				order.setSettleTime(settleTime);
				order.setStatus(Config.ORDER_PAID);
				session.saveOrUpdate(order);
				t.commit();
			}else if(Config.ORDER_PAID != status){//除了2，3状态外其他状态被通知到都是非法的。
				//TODO 增加报警机制
				resp.setErrResp(Error.ORDER_STATUS_ERR_CD, Error.ORDER_STATUS_ERR_MSG);
				return;
			}
		}catch (Exception e) {
			e.printStackTrace();
			t.rollback();
			resp.setErrResp(Error.SERVER_SYS_ERR_CD, Error.SERVER_SYS_ERR_MSG);
			return;
		}finally{
			session.close();
		}
		//正常情况下不需要返回任何内容
		return;
	}

}
